package com.flyoil.petromp.mvp_m.CourseMessageModel;

import com.cnpc.fyutils.NumberToWord;
import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.R;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.entity.entity_course_message.ServiceExamineMessageEntity;
import com.flyoil.petromp.entity.entity_order.MapListEntity;
import com.flyoil.petromp.entity.entity_order.OrderGoodsEntity;
import com.flyoil.petromp.entity.entity_order.OrderSignatureEntity;
import com.flyoil.petromp.base.BaseModel;
import com.flyoil.petromp.mvp_p.Listener.OnPresenterListener;
import com.flyoil.petromp.mvp_p.Listener.OrderMessageListener;
import com.flyoil.petromp.utils.MoccaPreferences;
import com.flyoil.petromp.utils.OrderUtl.OrderStatusUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by songzhiyin on 2017/10/31.
 * 服务验收单的详情界面的数据处理类
 */

public class ServiceExamineMessageModel implements BaseModel {


    @Override
    public void loadNetworkData(Map<String, Object> map, final OnPresenterListener onPresenterListener) {
        httpModel.getCourseServiceExamineMessage(map, new OnObserverRetrofitResetCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity model) {
                super.onSuccess(model);
                if (model.getCode() != ConstantNetworkCode.SuccedCode) {
                    onPresenterListener.onsucced(null);
                    return;
                }
                ServiceExamineMessageEntity entity = null;
                if (model instanceof ServiceExamineMessageEntity) {
                    entity = (ServiceExamineMessageEntity) model;
                }
                if (entity != null && entity.getData() != null) {
                    OrderMessageListener listener = (OrderMessageListener) onPresenterListener;
                    //基本信息
                    listener.upDataTitleName(R.mipmap.message_basic_icon,"基本信息");
                    List<MapListEntity> mapListEntities = new ArrayList<>();
                    mapListEntities.add(new MapListEntity("需求名称", entity.getData().getServiceRequirementName()));
                    mapListEntities.add(new MapListEntity("订单编号", entity.getData().getServiceOrderSn()));
                    mapListEntities.add(new MapListEntity("申请人", entity.getData().getUsername()));
                    mapListEntities.add(new MapListEntity("供应商", entity.getData().getSupplierName()));
                    mapListEntities.add(new MapListEntity("供应商电话", entity.getData().getSupplierPhone()));
                    mapListEntities.add(new MapListEntity("实际完成日期", entity.getData().getAppliedTimeLabel()));
                    mapListEntities.add(new MapListEntity("进度描述", entity.getData().getDescription()));
                    mapListEntities.add(new MapListEntity("本次付款金额", entity.getData().getPayMonyeLabel()));
                    mapListEntities.add(new MapListEntity("累计付款金额", entity.getData().getTotalPayMoneyLabel()));
                    mapListEntities.add(new MapListEntity("累计付款率", entity.getData().getTotalPayRateLabel()));
                    listener.upDataTextsView(mapListEntities);
                    //对合同实际履行的说明的备注
                    listener.upDataRemarkView(new MapListEntity("对合同实际履行的说明", entity.getData().getContractDesc()));
                    //验收执行的标准和规范的备注
                    listener.upDataRemarkView(new MapListEntity("验收执行的标准和规范", entity.getData().getSpecification()));
                    //服务明细
                    listener.upDataTitleName(R.mipmap.message_project_icon,"服务验收明细");
                    List<OrderGoodsEntity> orderGoodsEntities = new ArrayList<>();
                    if (entity.getData().getServiceAcceptanceItems() != null) {
                        for (int i = 0; i < entity.getData().getServiceAcceptanceItems().size(); i++) {
                            List<MapListEntity> mapListEntities1 = new ArrayList<>();
                            mapListEntities1.add(new MapListEntity("服务内容", entity.getData().getServiceAcceptanceItems().get(i).getServiceContent()));
                            mapListEntities1.add(new MapListEntity("计费金额", "￥" + entity.getData().getServiceAcceptanceItems().get(i).getPayMoneyLabel()));
                            orderGoodsEntities.add(new OrderGoodsEntity(entity.getData().getServiceAcceptanceItems().get(i).getId(), mapListEntities1));
                        }
                    }
                    listener.upDataGoodsView(orderGoodsEntities);

                    listener.upDataMoneyView(new MapListEntity("合计金额(大写)", NumberToWord.transfer(entity.getData().getTotalMoneyLabel())));
                    //签字意见并获取签字意见列表未录入意见的数据最大的id
                    if (entity.getData().getConfirmLogs() != null) {
                        List<OrderSignatureEntity> confirmLogs = new ArrayList<OrderSignatureEntity>();
                        int sigantureId = 0;
                        for (int i = 0; i < entity.getData().getConfirmLogs().size(); i++) {
                            OrderSignatureEntity sigantureEntity = entity.getData().getConfirmLogs().get(i);
                            if (sigantureEntity.getAppliedTimeLabel() != null && sigantureEntity.getAppliedTimeLabel().length() > 0) {
                                confirmLogs.add(sigantureEntity);
                            } else {
                                if (sigantureEntity.getId() < sigantureId || sigantureId == 0) {
                                    sigantureId = sigantureEntity.getId();
                                }
                            }
                        }
                        //更新签字意见列表
                        listener.upDataSiganturesView(confirmLogs);
                        //更新单据的签字意见的id
                        listener.upDataOrderConfirmId(sigantureId);
                    }
                    //审核状态为待审核，单据状态流转中，显示签字意见输入框
                    if (entity.getData().getTransformStatusLabel().equals(OrderStatusUtil.FLOW_FLOW) && entity.getData().getConfirmStatusLabel().equals(OrderStatusUtil.STATUS_UNAPPROVE)) {
                        listener.upDataShowSigantureView(true);
                    } else {
                        listener.upDataShowSigantureView(false);
                    }
                    //审核状态为已驳回待撤销、单据流转状态是待撤销、当前登录用户是申请人，才能显示撤销按钮
                    if (entity.getData().getUserId() == MoccaPreferences.userId.get()&&entity.getData().getTransformStatusLabel().equals(OrderStatusUtil.FLOW_UNBACKOUT) && entity.getData().getConfirmStatusLabel().equals(OrderStatusUtil.STATUS_UNBACKOUT)) {
                        listener.upDataShowBackoutView(true);
                    } else {
                        listener.upDataShowBackoutView(false);
                    }
                    //审核状态为已撤销时，显示已撤销图标
                    if (entity.getData().getConfirmStatusLabel().equals(OrderStatusUtil.STATUS_BACKOUTED)) {
                        listener.upDataShowRemoveImagView(true);
                    } else {
                        listener.upDataShowRemoveImagView(false);
                    }
                } else {
                    onPresenterListener.onsucced(null);
                }
            }

            @Override
            public void onFailure(String msg) {
                onPresenterListener.onsucced(null);
//                setdata(onPresenterListener);
            }
        });


    }

    private void setdata(OnPresenterListener onPresenterListener) {
        OrderMessageListener listener = (OrderMessageListener) onPresenterListener;
        List<MapListEntity> mapListEntities = new ArrayList<>();
        mapListEntities.add(new MapListEntity("需求名称", "网络服务项目"));
        mapListEntities.add(new MapListEntity("订单编号", "ES25412562"));
        mapListEntities.add(new MapListEntity("申请人", "李华"));
        mapListEntities.add(new MapListEntity("供应商", "托肯恒山有限公司"));
        mapListEntities.add(new MapListEntity("供应商电话", "15801126223"));
        mapListEntities.add(new MapListEntity("标准和规范", "北京康特尔有限公司"));
        mapListEntities.add(new MapListEntity("实际完成日期", "2017-10-15"));
        mapListEntities.add(new MapListEntity("进度描述", "进度完成正常"));
        mapListEntities.add(new MapListEntity("本次付款金额", "600元"));
        mapListEntities.add(new MapListEntity("累计付款金额", "600元"));
        mapListEntities.add(new MapListEntity("累计付款率", "50%"));
        listener.upDataTextsView(mapListEntities);

        //对合同实际履行的说明的备注
        listener.upDataRemarkView(new MapListEntity("对合同实际履行的说明", "合同履行的很好"));
        //验收执行的标准和规范的备注
        listener.upDataRemarkView(new MapListEntity("验收执行的标准和规范", "规范就是认定的"));

        List<OrderGoodsEntity> orderGoodsEntities = new ArrayList<>();
        List<MapListEntity> mapListEntities1 = new ArrayList<>();
        mapListEntities1.add(new MapListEntity("服务内容", "网络维护"));
        mapListEntities1.add(new MapListEntity("计费金额", "￥220"));
        orderGoodsEntities.add(new OrderGoodsEntity(mapListEntities1));
        listener.upDataGoodsView(orderGoodsEntities);

        listener.upDataMoneyView(new MapListEntity("合计金额(大写)", NumberToWord.transfer(1524100 + "")));

        List<OrderSignatureEntity> orderSignatureEntities = new ArrayList<>();
        orderSignatureEntities.add(new OrderSignatureEntity("王华"));
        orderSignatureEntities.add(new OrderSignatureEntity("李薇"));
        orderSignatureEntities.add(new OrderSignatureEntity("李薇"));
        orderSignatureEntities.add(new OrderSignatureEntity("赵莉"));
        listener.upDataSiganturesView(orderSignatureEntities);

        listener.upDataShowSigantureView(true);

        listener.upDataTitleName(R.mipmap.matter_goods_order_icon,"服务验收明细");
    }
}
